from PIL import Image

# 打开图片
img = Image.open(input("请输入需要转换字符画的图片路径："))
# 改变图片颜色，L为灰度
out = img.convert("L")
# 获取图片大小
width, height = out.size
# 改变图片大小
out = out.resize((int(width * 0.5), int(height * 0.5 * 0.5)))
width, height = out.size
asciis = "@%#*+=-. "
texts = ""
for row in range(height):
  for col in range(width):
    gray = out.getpixel((col, row))
    texts = texts + asciis[int(gray / 255 * 8)]
  texts = texts + "\n"

with open("字符画.txt", "w") as f:
  f.write(texts)
